package com.suyuan.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.suyuan.dao.*;
import com.suyuan.entity.*;
import com.suyuan.service.TransitBatchInfoService;
import com.suyuan.vo.TransitBatchInfoVO;
import com.suyuan.web.Page;
import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * Created by yang on 2018/1/30.
 */
@Service(value = "transitBatchInfoService")
public class TransitBatchInfoServiceImpl implements TransitBatchInfoService {

    @Resource
    private TransitbatchInfoMapper transitbatchInfoMapper;
    @Resource
    private TransportrouteInforMapper transportrouteInforMapper;
    @Resource
    private CarinfoMapper carinfoMapper;
    @Resource
    private CompanyInfoMapper companyInfoMapper;
    @Resource
    private TransitInfoMapper transitMapper;

    /**
     * 线路批次列表
     * @return
     */
    public List<TransitBatchInfoVO> list() {
        List<TransitBatchInfoVO> transiBatchVOs = new ArrayList<TransitBatchInfoVO>();

        TransitBatchInfoExample example = new TransitBatchInfoExample();
        List<TransitBatchInfo> batches = transitbatchInfoMapper.selectByExample(example);

        for(TransitBatchInfo batchInfo : batches ){
            //路线
            TransportRouteInfor route = transportrouteInforMapper.selectByPrimaryKey(batchInfo.getRouteId());
            //货车
            CarInfo car = carinfoMapper.selectByPrimaryKey(batchInfo.getTruckId());
            //物流公司
            CompanyInfo company = companyInfoMapper.selectByPrimaryKey(batchInfo.getCompId());
            //添加进VO集合中
            //transiBatchVOs.add(new TransitBatchInfoVO(batchInfo,route,company,car));
        }
        return null;
    }

    /**
     * 删除
     * @param id
     * @return
     */
    public int delete(Integer id) {
        //先删除该id绑定的所有外键表，transit_infor物流信息
        TransitInfoExample example = new TransitInfoExample();
        TransitInfoExample.Criteria criteria = example.createCriteria();
        criteria.andTransitbatchidEqualTo(id);
        transitMapper.deleteByExample(example);
        //删除批次
        int code = transitbatchInfoMapper.deleteByPrimaryKey(id);
        return code;
    }

    /**
     * 插入
     * @param transitBatchInfo
     * @return
     */
    public int insert(TransitBatchInfo transitBatchInfo) {
        int code = transitbatchInfoMapper.insert(transitBatchInfo);
        return code;
    }

    /**
     * 线路集合
     */
    public List<TransportRouteInfor> getRoutes(){
        return transportrouteInforMapper.selectByExample(new TransportRouteInforExample());
    }

    /**
     * 货车集合
     */
    public List<CarInfo> getCars(){
        return carinfoMapper.selectByExample(new CarInfoExample());
    }

    /**
     * 物流公司集合
     */
    public List<CompanyInfo> getCompanies(){
        return companyInfoMapper.selectByExample(new CompanyInfoExample());
    }

    /**
     * 条件、分页线路列表
     * @param batchId
     * @param routeId
     * @param page
     * @return
     */
    public PageInfo inquiry(String batchId, String routeId, Page page) {
        List<TransitBatchInfoVO> vos = new ArrayList<TransitBatchInfoVO>();
        if(StringUtils.isBlank(batchId)){
            batchId = null;
        }
        if(StringUtils.isBlank(routeId)){
            routeId = null;
        }

        //批次信息
        PageHelper.startPage(page.getPageNumber(),page.getPageSize());
        List<TransitBatchInfo> batches = transitbatchInfoMapper.likeInquiry(batchId,routeId);
        PageInfo info = new PageInfo(batches);
        for(TransitBatchInfo batch : (List<TransitBatchInfo>)info.getList()){
            TransportRouteInfor route = transportrouteInforMapper.selectByPrimaryKey(batch.getRouteId());
            CarInfo car = carinfoMapper.selectByPrimaryKey(batch.getTruckId());
            CompanyInfo company = companyInfoMapper.selectByPrimaryKey(batch.getCompId());
            //vos.add(new TransitBatchInfoVO(batch,route,company,car));
        }
        info.setList(vos);
        return null;
    }

    public TransitBatchInfo findById(Integer id) {
        TransitBatchInfo batch = transitbatchInfoMapper.selectByPrimaryKey(id);
        return batch;
    }

    public int update(TransitBatchInfo batchInfo) {
        return transitbatchInfoMapper.updateByPrimaryKey(batchInfo);
    }
}
